package cn.itcast.bos.web.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.common.entity.EasyUIDatagridResult;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;
import oracle.net.aso.s;

/**
 * 收派标准模块
 * @author Beck
 *
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends BaseAction<Standard> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private StandardService standardService;
	
	/**
	 * 添加 收派标准
	 * @return
	 * @throws Exception
	 */
	@Action(value="standardAction_add",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String standardAdd() throws Exception {
		standardService.addStandard(model);
		return SUCCESS;
	}
	
	/**
	 * 根据名称查询 收派标准
	 * @return
	 * @throws Exception
	 */
	@Action(value="standardAction_validate",results={@Result(name="success",type="json")})
	public String standardValidateName() throws Exception {
		List<Standard> list = standardService.findStandardByName(model.getName());
		if (list != null && list.size() == 0){
			// 说明没有数据，返回true
			ServletActionContext.getContext().getValueStack().push(true);;
		}else {
			// 查询到数据，返回false
			ServletActionContext.getContext().getValueStack().push(false);;
		}
		return SUCCESS;
	}
	
	/**
	 * 分页查询
	 */
	@Action(value="standardAction_page",results={@Result(name="success",type="json")})
	public String standardListByPage() throws Exception {
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Standard> standardPage = standardService.findStandardByPage(pageable);
		EasyUIDatagridResult result = new EasyUIDatagridResult(standardPage.getTotalElements(), standardPage.getContent());
		ServletActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
	/**
	 * 查询所有的 取派标准
	 */
	@Action(value="standardAction_findAll",results={@Result(name="success",type="json")})
	public String standardFindAll() throws Exception {
		List<Standard> list = standardService.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	/**
	 * 修改 取派标准
	 */
	@Action(value="standardAction_update",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String standardUpdate() throws Exception {
		standardService.updateStandard(model);
		return SUCCESS;
	}
	
	// 获取批量删除的所有id
	private String ids;
	public void setIds(String ids) {
		this.ids = ids;
	}
	
	/**
	 * 删除 取派标准
	 */
	@Action(value="standardAction_delete",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String standardDelete() throws Exception {
		String[] idArr = ids.split(",");
		if (idArr != null && idArr.length > 0){
			standardService.deleteStandard(idArr);
		}
		return SUCCESS;
	}
	
}
